package com.cnebula.openapi.auth;

import com.cnebula.openapi.auth.api.AuthAPIVerticle;
import com.cnebula.openapi.auth.provider.LoginProvider;
import com.cnebula.openapi.auth.provider.OAuthProvider;
import com.cnebula.openapi.auth.service.ClientAppService;
import com.cnebula.openapi.auth.service.OAuthService;
import com.cnebula.openapi.auth.service.SignService;
import com.cnebula.openapi.auth.service.impl.ClientAppServiceImpl;
import com.cnebula.openapi.auth.service.impl.OAuthServiceImpl;
import com.cnebula.openapi.auth.service.impl.SignServiceImpl;
import com.cnebula.openapi.auth.util.Runner;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Future;
import io.vertx.core.json.JsonObject;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;

public class MainVerticle extends AbstractVerticle {

	private static final Logger log = LoggerFactory.getLogger(MainVerticle.class);

	@Override
	public void start(Future<Void> startFuture) throws Exception {
		JsonObject config = context.config();
		String host = System.getProperty(AuthOptions.CLI_HOST);
		String port = System.getProperty(AuthOptions.CLI_PORT);
		String lspHost = System.getProperty(AuthOptions.CLI_LSP_HOST);
		String lspPort = System.getProperty(AuthOptions.CLI_LSP_PORT);
		String okHost = System.getProperty(AuthOptions.CLI_OKAPI_HOST);
		String okPort = System.getProperty(AuthOptions.CLI_OKAPI_PORT);
		String envDEV = System.getProperty(AuthOptions.CLI_ENV_DEV);
		String redisHost = System.getProperty(AuthOptions.CLI_REIDS_HOST);
		String redisPort = System.getProperty(AuthOptions.CLI_REIDS_PORT);

		if (host != null && !host.isEmpty()) {
			config.put(AuthOptions.CLI_HOST, host);
		}
		if (port != null && !port.isEmpty()) {
			config.put(AuthOptions.CLI_PORT, Integer.parseInt(port));
		}
		if (lspHost != null && !lspHost.isEmpty()) {
			config.put(AuthOptions.CLI_LSP_HOST, lspHost);
		}
		if (lspPort != null && !lspPort.isEmpty()) {
			config.put(AuthOptions.CLI_LSP_PORT, Integer.parseInt(lspPort));
		}
		if (host != null && !host.isEmpty()) {
			config.put(AuthOptions.CLI_OKAPI_HOST, okHost);
		}
		if (port != null && !port.isEmpty()) {
			config.put(AuthOptions.CLI_OKAPI_PORT, Integer.parseInt(okPort));
		}
		if (envDEV != null && envDEV.equals("yes")) {
			config.put(AuthOptions.CLI_ENV_DEV, envDEV);
		}
		if (redisHost != null && !lspHost.isEmpty()) {
			config.put(AuthOptions.CLI_REIDS_HOST, redisHost);
		}
		if (redisPort != null && !lspPort.isEmpty()) {
			config.put(AuthOptions.CLI_REIDS_PORT, Integer.parseInt(redisPort));
		}
		

		AuthOptions options = new AuthOptions(config);

		SignService signService = new SignServiceImpl();
		OAuthService oauthService = new OAuthServiceImpl(vertx, options);
		ClientAppService clientAppService = new ClientAppServiceImpl(vertx, options);

		LoginProvider loginProvider = new LoginProvider(signService, oauthService, clientAppService,
				options);
		OAuthProvider oauthProvider = new OAuthProvider(signService, oauthService, clientAppService,
				options);
		
		if(options.getEnvDev().equals("yes")) {
			log.info("start up dev mode");
			vertx.deployVerticle(RedisServerVerticle.class.getName(), new DeploymentOptions());
		}else {
			log.info("Redis register at [host:"+options.getRedisHost()+", port:"+options.getRedisPort()+"]");
		}
		
		log.info("LSP register at [http://"+options.getLspHost()+":"+options.getLspPort()+"]");
		log.info("OKAPI register at [http://"+options.getOkapiHost()+":"+options.getOkapiPort()+"]");

		vertx.deployVerticle(new AuthAPIVerticle(loginProvider, oauthProvider, options), new DeploymentOptions(), e -> {
			if (e.succeeded()) {
				startFuture.complete();
			} else {
				startFuture.fail(e.cause());
			}
		});
	}

	@Override
	public void stop(Future<Void> stopFuture) throws Exception {
		super.stop(stopFuture);
	}

	public static void main(String[] args) {
		Runner.deploy(MainVerticle.class);
	}
}
